home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / RemoteObject.java < prev    next >
Text File  |  1998-09-22  |  4KB  |  133 lines

  1. /*
  2.  * @(#)RemoteObject.java    1.7 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.rmi.server;
  16.  
  17. import java.rmi.Remote;
  18. import java.rmi.UnmarshalException;
  19.  
  20. /**
  21.  * The RemoteObject class implements the java.lang.Object behavior for
  22.  * remote objects.  RemoteObject provides the remote semantics of
  23.  * Object by implementing methods for hashCode, equals, and toString.
  24.  */
  25. public abstract class RemoteObject implements Remote, java.io.Serializable {
  26.  
  27.     transient protected RemoteRef ref;
  28.     
  29.     /**
  30.      * Create a remote object.
  31.      */
  32.     protected RemoteObject() {
  33.     ref = null;
  34.     }
  35.     
  36.     /**
  37.      * Create a remote object, initialized with the specified remote reference.
  38.      */
  39.     protected RemoteObject(RemoteRef newref) {
  40.     ref = newref;
  41.     }
  42.  
  43.     /**
  44.      * Returns a hashcode for a remote object.  Two remote object stubs
  45.      * that refer to the same remote object will have the same hash code
  46.      * (in order to support remote objects as keys in hash tables).
  47.      *
  48.      * @see        java.util.Hashtable
  49.      */
  50.     public int hashCode() {
  51.     return (ref == null) ? super.hashCode() : ref.remoteHashCode();
  52.     }
  53.  
  54.     /**
  55.      * Compares two remote objects for equality.
  56.      * Returns a boolean that indicates whether this remote object is
  57.      * equivalent to the specified Object. This method is used when a
  58.      * remote object is stored in a hashtable.
  59.      * @param    obj    the Object to compare with
  60.      * @return    true if these Objects are equal; false otherwise.
  61.      * @see        java.util.Hashtable
  62.      */
  63.     public boolean equals(Object obj) {
  64.     if (obj instanceof RemoteObject) {
  65.         if (ref == null) {
  66.         return obj == this;
  67.         } else {
  68.         return ref.remoteEquals(((RemoteObject)obj).ref);
  69.         }
  70.     } else if (obj != null) {
  71.         /*
  72.          * Fix for 4099660: if object is not an instance of RemoteObject,
  73.          * use the result of its equals method, to support symmetry if a
  74.          * remote object implementation class that does not extend
  75.          * RemoteObject wishes to support equality with its stub objects.
  76.          */
  77.         return obj.equals(this);
  78.     } else {
  79.         return false;
  80.     }
  81.     }
  82.  
  83.     /**
  84.      * Returns a String that represents the value of this remote object.
  85.      */
  86.     public String toString()
  87.     {
  88.     String classname = this.getClass().getName();
  89.     return (ref == null) ? classname :
  90.         classname + "[" +ref.remoteToString() + "]";
  91.     }
  92.  
  93.  
  94.     /**
  95.      * writeObject for object serialization. Writes out the class name of
  96.      * the remote reference and delegates to the reference to write out
  97.      * its representation.
  98.      */
  99.     private void writeObject(java.io.ObjectOutputStream out)
  100.     throws java.io.IOException, java.lang.ClassNotFoundException
  101.     {
  102.     if (ref == null) {
  103.         throw new java.rmi.MarshalException("Invalid remote object");
  104.     } else {
  105.         out.writeUTF(ref.getRefClass(out));
  106.         ref.writeExternal(out);
  107.     }
  108.     
  109.     }
  110.  
  111.     /**
  112.      * readObject for object serialization. Reads in the class name of
  113.      * the remote reference and delegates to the reference to read in
  114.      * its representation.
  115.      */
  116.     private void readObject(java.io.ObjectInputStream in) 
  117.     throws java.io.IOException, java.lang.ClassNotFoundException
  118.     {
  119.     try {
  120.         Class refClass = Class.forName(RemoteRef.packagePrefix + "." +
  121.                        in.readUTF());
  122.         ref = (RemoteRef)refClass.newInstance();
  123.         ref.readExternal(in);
  124.     } catch (InstantiationException e) {
  125.         throw new UnmarshalException("Unable to create remote reference",
  126.                      e);
  127.     } catch (IllegalAccessException e) {
  128.         throw new UnmarshalException("Illegal access creating remote reference");
  129.     }
  130.     }
  131.  
  132. }
  133.